import md5 from"crypto-js/md5";

export default{
    data (){
       return {
            list: [],
            arr:[5,10,15,20],
            total: 0,
            page: 1,
            limit:0,
            
        }
    },
    methods:{
        getData(){
            this.filter.page=this.page;
            this.filter.limit=this.limit;
            this.filter.rows=this.filter.limit;
            this.$http.post(`http://10.31.160.25:3000/${this.module}/list`,this.filter).then(res=> {
                this.list=res.data.rows;
                this.total=res.data.total;
                this.page=res.data.page;
                
            })
        },
        
        remove (index) {
            
            this.$http.delete(`http://10.31.160.25:3000/${this.module}/data/${this.list[index]._id}`).then(res=> {
                this.list.splice(index, 1);
                this.$Message.success('Success!');  
                this.getData();
            })
        },
        confirm () {
            this.$Modal.confirm({
                title: '确认删除？',
                content: '是否确认删除',
                onOk: () => {
                    if(!this.selectData){
                        this.$Message.error('删除目标未选定'); 
                    }
                    this.manyDelete();
                },
                onCancel: () => {
                    this.$Message.info('取消删除');
                }
            });
        },
        manyDelete(){
            var ids=[];
            for(var i=0;i<this.selectData.length;i++){
                ids.push(this.selectData[i]._id);
            }
           
            if(this.list.length==ids.length&&this.page!=1){
                this.page-=1
            }
           
             this.$http.post(`http://10.31.160.25:3000/${this.module}/data/removes`,{ids:ids.toString()}).then(res=>{
                this.$Modal.success({
                    title: 'delete success',
                    content: '删除成功'
                    
                })
              this.getData();
             });
            
        },
         addSelectData(event){
          this.selectData=event;
        },
        search(){
            this.getData();
        },
        pageChange(event){
           this.page=event;
           this.getData();
        },
        pageSizeChange(event){
            this.limit=event;
            this.getData();
        },
        ok () {
            this.$Message.info('Clicked ok');
             this.getData();
           
        },
        cancel () {
            this.$Message.info('Clicked cancel');
        },
        update(index){
            this.$http.put(`http://10.31.160.25:3000/${this.module}/data/${this.list[index]._id}`).then(res=>{
                
            })
        },
        handleSubmit (name) {
            var that=this;
            this.$refs[name].validate((valid) => {
                if (valid) {
                   if( this.formValidate.password) { this.formValidate.password= md5(this.formValidate.password).toString()}
                    if(this.type=="create"){
                        this.formValidate._id=null;
                        this.$http.post(`http://10.31.160.25:3000/${this.module}/data`,this.formValidate).then(res=>{
                            this.$Message.success('添加成功!');
                            this.modal=false;
                            that.$refs[name].resetFields();
                            this.getData();
                        })
                    } 
                    if(this.type=="change"){ 
                        this.$http.put(`http://10.31.160.25:3000/${this.module}/data/${this.formValidate._id}`,this.formValidate).then(res=>{
                            this.$Message.success("修改成功");
                            this.modal=false;
                            that.$refs[name].resetFields();
                            this.getData();
                        })
                    }    
                } else {
                    this.$Message.error('Fail!');
                }
            })
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        }
    },
    created(){
        this.getData()
    }   
}